//搜索页面
import 'package:flutter/material.dart';
import 'package:wan_android/pages/item_artical.dart';
import 'package:wan_android/pages/page_hot.dart';

class PageSearch extends StatefulWidget {
  final String editStr;

  PageSearch({this.editStr});

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return PageSearchState();
  }
}

class PageSearchState extends State<PageSearch> {
  String text;

  TextEditingController controller;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    controller = new TextEditingController(text: widget.editStr);
    text = widget.editStr;
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    print('重建search页面');
    return Scaffold(
        appBar: AppBar(
          title: TextField(
            decoration: InputDecoration(
              hintText: '搜索关键词',
              border: InputBorder.none,
              focusedBorder: UnderlineInputBorder(
                borderSide: BorderSide(
                  color: Colors.red,
                  width: 1.0,
                ),
              ),
            ),
            autofocus: false,
            controller: controller,
          ),
          actions: <Widget>[
            IconButton(
                icon: Icon(
                  Icons.search,
                  color: Colors.blue,
                ),
                onPressed: () {
                  print('搜索');
                  _clickSearch();
                }),
            IconButton(
                icon: Icon(
                  Icons.clear,
                  color: Colors.blue,
                ),
                onPressed: () {
                  _clickCancel();
                }),
          ],
        ),
        body: text == null || text.isEmpty
            ? PageHot()
            : ItemWidget(
                //http://www.wanandroid.com/article/query/0/json
                firApi: 'http://www.wanandroid.com/article/query/',
                secApi: '/json',
                isFavorite: false,
                isSearch: true,
                searchStr: controller.text.toString(),
              ));
  }

  //点击搜索按钮
  void _clickSearch() {
    setState(() {
      text = controller.text.toString();
    });
  }

  //点击取消
  void _clickCancel() {
    setState(() {
      controller.clear();
      text = null;
    });
  }
}
